Dashboard Temp Share Shortlinks Frames API

HTMLify

Best time to buy and sell stock.cpp
Views: 2 | Author: cody
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include<bits/stdc++.h>
using namespace std;
int bestTimeToBuyAndSellStock(vector<int>&prices) {
    // Write your code here.
    int mini = prices[0];
    int maxProfit = 0;
    int n = prices.size();

    for(int i = 1; i<n; i++){
        int cost = prices[i] - mini;
        maxProfit = max(maxProfit, cost);
        mini = min(mini, prices[i]);
    }
    return maxProfit;
}